{ *
  * Copyright (C) 2012-2013 ozok <ozok26@gmail.com>
  *
  * This file is part of TAudioConverter.
  *
  * TAudioConverter is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, either version 2 of the License, or
  * (at your option) any later version.
  *
  * TAudioConverter is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with TAudioConverter.  If not, see <http://www.gnu.org/licenses/>.
  *
  * }

unit UnitLog;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, JvExStdCtrls, JvListBox, Vcl.ComCtrls, sSkinProvider,
  Vcl.ExtCtrls, sPanel, sListBox, Vcl.Buttons, sBitBtn, JvExControls,
  JvEditorCommon, JvUnicodeEditor, JvUnicodeHLEditor, JvXPCore, JvXPButtons,
  acPNG, sComboBox;

type
  TLogForm = class(TForm)
    SaveBtn: TsBitBtn;
    ClearBtn: TsBitBtn;
    SaveDialog1: TSaveDialog;
    CloseBtn: TsBitBtn;
    sSkinProvider1: TsSkinProvider;
    OutputList: TMemo;
    EncoderOutput1: TMemo;
    EncoderOutput2: TMemo;
    EncoderOutput3: TMemo;
    EncoderOutput4: TMemo;
    EncoderOutput5: TMemo;
    EncoderOutput6: TMemo;
    EncoderOutput7: TMemo;
    EncoderOutput8: TMemo;
    LogList: TsComboBox;
    ClearAllBtn: TsBitBtn;
    sBitBtn1: TsBitBtn;
    DeletedLog: TMemo;
    procedure ClearBtnClick(Sender: TObject);
    procedure SaveBtnClick(Sender: TObject);
    procedure CloseBtnClick(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure OutputListAddString(Sender: TObject; Item: string);
    procedure OutputListDrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    procedure FullOutputListDrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    procedure LogListChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ClearAllBtnClick(Sender: TObject);
    procedure sBitBtn1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  LogForm: TLogForm;

implementation

{$R *.dfm}

uses UnitMain;

procedure TLogForm.ClearAllBtnClick(Sender: TObject);
begin

  OutputList.Lines.Clear;
  EncoderOutput1.Lines.Clear;
  EncoderOutput2.Lines.Clear;
  EncoderOutput3.Lines.Clear;
  EncoderOutput4.Lines.Clear;
  EncoderOutput5.Lines.Clear;
  EncoderOutput6.Lines.Clear;
  EncoderOutput7.Lines.Clear;
  EncoderOutput8.Lines.Clear;
  DeletedLog.Lines.Clear;

end;

procedure TLogForm.ClearBtnClick(Sender: TObject);
begin

  case LogList.ItemIndex of
    0:
      OutputList.Lines.Clear;
    1:
      EncoderOutput1.Lines.Clear;
    2:
      EncoderOutput2.Lines.Clear;
    3:
      EncoderOutput3.Lines.Clear;
    4:
      EncoderOutput4.Lines.Clear;
    5:
      EncoderOutput5.Lines.Clear;
    6:
      EncoderOutput6.Lines.Clear;
    7:
      EncoderOutput7.Lines.Clear;
    8:
      EncoderOutput8.Lines.Clear;
    9:
      DeletedLog.Lines.Clear;
  end;

end;

procedure TLogForm.CloseBtnClick(Sender: TObject);
begin

  Self.Close;

end;

procedure TLogForm.FormCreate(Sender: TObject);
begin

  LogList.OnChange(Self);

end;

procedure TLogForm.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin

  if Key = VK_ESCAPE then
  begin
    CloseBtn.OnClick(Self);
  end;

end;

procedure TLogForm.FormShow(Sender: TObject);
begin

  OutputList.Lines.Clear;
  if FileExists(MainForm.AppData + '\log_main.txt') then
  begin
    OutputList.Lines.LoadFromFile(MainForm.AppData + '\log_main.txt');
  end;
  EncoderOutput1.Lines.Clear;
  if FileExists(MainForm.AppData + '\log_encoder1.txt') then
  begin
    EncoderOutput1.Lines.LoadFromFile(MainForm.AppData + '\log_encoder1.txt');
  end;
  EncoderOutput2.Lines.Clear;
  if FileExists(MainForm.AppData + '\log_encoder2.txt') then
  begin
    EncoderOutput2.Lines.LoadFromFile(MainForm.AppData + '\log_encoder2.txt');
  end;
  EncoderOutput3.Lines.Clear;
  if FileExists(MainForm.AppData + '\log_encoder3.txt') then
  begin
    EncoderOutput3.Lines.LoadFromFile(MainForm.AppData + '\log_encoder3.txt');
  end;
  EncoderOutput4.Lines.Clear;
  if FileExists(MainForm.AppData + '\log_encoder4.txt') then
  begin
    EncoderOutput4.Lines.LoadFromFile(MainForm.AppData + '\log_encoder4.txt');
  end;
  EncoderOutput5.Lines.Clear;
  if FileExists(MainForm.AppData + '\log_encoder5.txt') then
  begin
    EncoderOutput5.Lines.LoadFromFile(MainForm.AppData + '\log_encoder5.txt');
  end;
  EncoderOutput6.Lines.Clear;
  if FileExists(MainForm.AppData + '\log_encoder6.txt') then
  begin
    EncoderOutput6.Lines.LoadFromFile(MainForm.AppData + '\log_encoder6.txt');
  end;
  EncoderOutput7.Lines.Clear;
  if FileExists(MainForm.AppData + '\log_encoder7.txt') then
  begin
    EncoderOutput7.Lines.LoadFromFile(MainForm.AppData + '\log_encoder7.txt');
  end;
  EncoderOutput8.Lines.Clear;
  if FileExists(MainForm.AppData + '\log_encoder8.txt') then
  begin
    EncoderOutput8.Lines.LoadFromFile(MainForm.AppData + '\log_encoder8.txt');
  end;
  DeletedLog.Lines.Clear;
  if FileExists(MainForm.AppData + '\log_deleted.txt') then
  begin
    DeletedLog.Lines.LoadFromFile(MainForm.AppData + '\log_deleted.txt');
  end;

end;

procedure TLogForm.FullOutputListDrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin

  if (Control as TsListBox).Items.Count < 1 then
    Exit;

  with Control as TsListBox, Canvas do
  begin

    // item selected
    if odSelected in State then
    begin

      Brush.Color := Self.Color;
      Font.Color := clBlack;
      FillRect(Rect);
      TextOut(Rect.Left + 2, Rect.Top + 2, Items[Index])

    end
    else
    begin
      // item not selected
      Brush.Color := (Control as TsListBox).Color;
      Font.Color := (Control as TsListBox).Font.Color;
      FillRect(Rect);
      TextOut(Rect.Left + 2, Rect.Top + 2, Items[Index])
    end;

  end;

end;

procedure TLogForm.LogListChange(Sender: TObject);
begin

  case LogList.ItemIndex of
    0:
      begin
        OutputList.Visible := True;
        EncoderOutput1.Visible := False;
        EncoderOutput2.Visible := False;
        EncoderOutput3.Visible := False;
        EncoderOutput4.Visible := False;
        EncoderOutput5.Visible := False;
        EncoderOutput6.Visible := False;
        EncoderOutput7.Visible := False;
        EncoderOutput8.Visible := False;
        DeletedLog.Visible := False;
      end;
    1:
      begin
        OutputList.Visible := False;
        EncoderOutput1.Visible := True;
        EncoderOutput2.Visible := False;
        EncoderOutput3.Visible := False;
        EncoderOutput4.Visible := False;
        EncoderOutput5.Visible := False;
        EncoderOutput6.Visible := False;
        EncoderOutput7.Visible := False;
        EncoderOutput8.Visible := False;
        DeletedLog.Visible := False;
      end;
    2:
      begin
        OutputList.Visible := False;
        EncoderOutput1.Visible := False;
        EncoderOutput2.Visible := True;
        EncoderOutput3.Visible := False;
        EncoderOutput4.Visible := False;
        EncoderOutput5.Visible := False;
        EncoderOutput6.Visible := False;
        EncoderOutput7.Visible := False;
        EncoderOutput8.Visible := False;
        DeletedLog.Visible := False;
      end;
    3:
      begin
        OutputList.Visible := False;
        EncoderOutput1.Visible := False;
        EncoderOutput2.Visible := False;
        EncoderOutput3.Visible := True;
        EncoderOutput4.Visible := False;
        EncoderOutput5.Visible := False;
        EncoderOutput6.Visible := False;
        EncoderOutput7.Visible := False;
        EncoderOutput8.Visible := False;
        DeletedLog.Visible := False;
      end;
    4:
      begin
        OutputList.Visible := False;
        EncoderOutput1.Visible := False;
        EncoderOutput2.Visible := False;
        EncoderOutput3.Visible := False;
        EncoderOutput4.Visible := True;
        EncoderOutput5.Visible := False;
        EncoderOutput6.Visible := False;
        EncoderOutput7.Visible := False;
        EncoderOutput8.Visible := False;
        DeletedLog.Visible := False;
      end;
    5:
      begin
        OutputList.Visible := False;
        EncoderOutput1.Visible := False;
        EncoderOutput2.Visible := False;
        EncoderOutput3.Visible := False;
        EncoderOutput4.Visible := False;
        EncoderOutput5.Visible := True;
        EncoderOutput6.Visible := False;
        EncoderOutput7.Visible := False;
        EncoderOutput8.Visible := False;
        DeletedLog.Visible := False;
      end;
    6:
      begin
        OutputList.Visible := False;
        EncoderOutput1.Visible := False;
        EncoderOutput2.Visible := False;
        EncoderOutput3.Visible := False;
        EncoderOutput4.Visible := False;
        EncoderOutput5.Visible := False;
        EncoderOutput6.Visible := True;
        EncoderOutput7.Visible := False;
        EncoderOutput8.Visible := False;
        DeletedLog.Visible := False;
      end;
    7:
      begin
        OutputList.Visible := False;
        EncoderOutput1.Visible := False;
        EncoderOutput2.Visible := False;
        EncoderOutput3.Visible := False;
        EncoderOutput4.Visible := False;
        EncoderOutput5.Visible := False;
        EncoderOutput6.Visible := False;
        EncoderOutput7.Visible := True;
        EncoderOutput8.Visible := False;
        DeletedLog.Visible := False;
      end;
    8:
      begin
        OutputList.Visible := False;
        EncoderOutput1.Visible := False;
        EncoderOutput2.Visible := False;
        EncoderOutput3.Visible := False;
        EncoderOutput4.Visible := False;
        EncoderOutput5.Visible := False;
        EncoderOutput6.Visible := False;
        EncoderOutput7.Visible := False;
        EncoderOutput8.Visible := True;
        DeletedLog.Visible := False;
      end;
    9:
      begin
        OutputList.Visible := False;
        EncoderOutput1.Visible := False;
        EncoderOutput2.Visible := False;
        EncoderOutput3.Visible := False;
        EncoderOutput4.Visible := False;
        EncoderOutput5.Visible := False;
        EncoderOutput6.Visible := False;
        EncoderOutput7.Visible := False;
        EncoderOutput8.Visible := False;
        DeletedLog.Visible := True;
      end;
  end;

end;

procedure TLogForm.OutputListAddString(Sender: TObject; Item: string);
begin

  // OutputList.TopIndex := OutputList.Items.Count - 1;

end;

procedure TLogForm.OutputListDrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin

  if (Control as TsListBox).Items.Count < 1 then
    Exit;

  with Control as TsListBox, Canvas do
  begin

    // item selected
    if odSelected in State then
    begin

      Brush.Color := Self.Color;
      Font.Color := clBlack;
      FillRect(Rect);
      TextOut(Rect.Left + 2, Rect.Top + 2, Items[Index])

    end
    else
    begin
      // item not selected
      Brush.Color := (Control as TsListBox).Color;
      Font.Color := (Control as TsListBox).Font.Color;
      FillRect(Rect);
      TextOut(Rect.Left + 2, Rect.Top + 2, Items[Index])
    end;

  end;

end;

procedure TLogForm.SaveBtnClick(Sender: TObject);
begin

  if OutputList.Lines.Count > 0 then
  begin
    if SaveDialog1.Execute then
    begin
      case LogList.ItemIndex of
        0:
          begin
            SaveDialog1.FileName := 'TAC_Main.txt';
            OutputList.Lines.SaveToFile(SaveDialog1.FileName);;
          end;
        1:
          begin
            SaveDialog1.FileName := 'TAC_Encoder1.txt';
            EncoderOutput1.Lines.SaveToFile(SaveDialog1.FileName);
          end;
        2:
          begin
            SaveDialog1.FileName := 'TAC_Encoder2.txt';
            EncoderOutput2.Lines.SaveToFile(SaveDialog1.FileName);
          end;
        3:
          begin
            SaveDialog1.FileName := 'TAC_Encoder3.txt';
            EncoderOutput3.Lines.SaveToFile(SaveDialog1.FileName);
          end;
        4:
          begin
            SaveDialog1.FileName := 'TAC_Encoder4.txt';
            EncoderOutput4.Lines.SaveToFile(SaveDialog1.FileName);
          end;
        5:
          begin
            SaveDialog1.FileName := 'TAC_Encoder5.txt';
            EncoderOutput5.Lines.SaveToFile(SaveDialog1.FileName);
          end;
        6:
          begin
            SaveDialog1.FileName := 'TAC_Encoder6.txt';
            EncoderOutput6.Lines.SaveToFile(SaveDialog1.FileName);
          end;
        7:
          begin
            SaveDialog1.FileName := 'TAC_Encoder7.txt';
            EncoderOutput7.Lines.SaveToFile(SaveDialog1.FileName);
          end;
        8:
          begin
            SaveDialog1.FileName := 'TAC_Encoder8.txt';
            EncoderOutput8.Lines.SaveToFile(SaveDialog1.FileName);
          end;
        9:
          begin
            SaveDialog1.FileName := 'TAC_Deleted.txt';
            DeletedLog.Lines.SaveToFile(SaveDialog1.FileName);
          end;
      end;

    end;
  end;

end;

procedure TLogForm.sBitBtn1Click(Sender: TObject);
var
  MSG: string;
begin

  MSG := 'When you wanted to send me logs, please send both TAudioConverter''s and one of the encoder''s log.'
    + sLineBreak + 'This way I can understand what happened with encoders.' +
    sLineBreak + 'Thank you.';
  Application.MessageBox(PChar(MSG), 'Info', MB_ICONINFORMATION);

end;

end.
